﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.ComponentModel;
using System.Web.Configuration;

namespace HospitalSystem
{
    public partial class findchild : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);





        }




        protected void Button1_Click(object sender, EventArgs e)
        {

            BindData();
        }
        protected void BindData()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            scon.Open();

            SqlCommand cmd = new SqlCommand("SELECT CID,FirstName,LastName FROM Person where CID=@text or FirstName=@text or Lastname=@text", scon);
            cmd.Parameters.AddWithValue("@text", TextBox1.Text);
            SqlDataReader reader = cmd.ExecuteReader();

                             
           

            //if (reader.Read())
            //{

                GridView1.DataSource = reader;
                GridView1.DataBind();
                scon.Close();

            //}
            //else
            //{
            //    MessageBox("ไม่พบการค้นหาครับ  กรุณาลงทะเบียนเด็กก่อนที่ปุ่มลงทะเบียน");
            //}
            scon.Close();
            reader.Close();

















        }
        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {

        }

        protected void BindDatadek()
        {
            SelectStat.Text = string.Empty;
            bool selectorChecked3 = false;

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("CID");
            dt2.Columns.Add("FirstName");
            dt2.Columns.Add("LastName");
        
           
            foreach (GridViewRow row in GridView1.Rows)
            {
                RadioButton cb = (RadioButton)row.FindControl("CaseSelector3");
                if (cb != null && cb.Checked)
                {
                    selectorChecked3 = true;

                    DataRow dr2;
                    dr2 = dt2.NewRow();
                    string CID = GridView1.Rows[row.RowIndex].Cells[0].Text;
                    string FirstName = GridView1.Rows[row.RowIndex].Cells[1].Text;
                    string LastName = GridView1.Rows[row.RowIndex].Cells[2].Text;
                    SelectStat.Text += string.Format(CID);
                    Label2.Text = string.Format(FirstName);
                    Label3.Text = string.Format(LastName);
                    dt2.Rows.Add(CID, FirstName, LastName);


                }
            }
            SelectStat.Visible = selectorChecked3;


        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void CaseSelector3_CheckedChanged(object sender, EventArgs e)
        {
            BindDatadek();
           // crepsnumber();
           // psnumbertolabel();
            sendsession();



        }

        protected void sendsession()
        {
            Session["CID"] = SelectStat.Text;
            Session["FirstName"] = Label2.Text;
            Session["LastName"] = Label3.Text;


            Response.Redirect("~/indexchild.aspx");


        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/reg.aspx");
        }

       

    }
}